﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Web/ActionController.cs
// Author: Sohobloo
// CreateTime: 2013/06/30 12:14
// UpdateTime: 2013/07/01 2:19

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoAuth.Service;
using Sohobloo.SoAuth.Web.Models;

#endregion

namespace Sohobloo.SoAuth.Web.Controllers
{
    [Export]
    public class ActionController : Controller
    {
        #region 属性

        [Import]
        public IActionService ActionService { get; set; }

        [Import]
        public IFunctionService FunctionService { get; set; }

        #endregion 属性

        #region 方法

        public ActionResult Add(Guid id)
        {
            var funtion = FunctionService.GetFunction(id);

            var ationModel = new ActionModel
            {
                FunctionId = id,
                FunctionName = funtion.FunctionName
            };
            return View(ationModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(ActionModel actionModel)
        {
            var action = new SoAuthAction
            {
                ActionName = actionModel.ActionName.Trim(),
                ActionPath = actionModel.ActionPath.Trim(),
                ActionEnabled = actionModel.ActionEnabled,
                IgnoreControl = actionModel.IgnoreControl,
                ActionFunction = FunctionService.GetFunction(actionModel.FunctionId)
            };

            ActionService.CreateAction(action);

            return RedirectToAction(
                "Index",
                new
                {
                    id = actionModel.FunctionId
                });
        }

        public ActionResult Delete(Guid id, Guid functionId)
        {
            ActionService.DeleteAction(id);

            return RedirectToAction(
                "Index",
                new
                {
                    id = functionId
                });
        }

        public ActionResult Edit(Guid id)
        {
            var action = ActionService.GetAction(id);

            var actionModel = new ActionModel
            {
                Id = action.Id,
                ActionEnabled = action.ActionEnabled,
                ActionName = action.ActionName,
                ActionPath = action.ActionPath,
                FunctionId = action.ActionFunction.Id,
                FunctionName = action.ActionFunction.FunctionName,
                IgnoreControl = action.IgnoreControl
            };

            return View(actionModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ActionModel actionModel)
        {
            var action = ActionService.GetAction(actionModel.Id);
            action.ActionName = actionModel.ActionName.Trim();
            action.ActionPath = actionModel.ActionPath.Trim();
            action.ActionEnabled = actionModel.ActionEnabled;
            action.IgnoreControl = actionModel.IgnoreControl;

            ActionService.UpdateAction(action);

            return RedirectToAction(
                "Index",
                new
                {
                    id = actionModel.FunctionId
                });
        }

        public ActionResult Index(Guid id)
        {
            var funtion = FunctionService.GetFunction(id);

            ViewBag.FunctionId = funtion.Id;
            ViewBag.FunctionName = funtion.FunctionName;

            var actionList = ActionService.GetActionList(id);

            var actionModelList = actionList.Select(
                a => new ActionModel
                {
                    Id = a.Id,
                    ActionEnabled = a.ActionEnabled,
                    ActionName = a.ActionName,
                    ActionPath = a.ActionPath,
                    FunctionId = funtion.Id,
                    FunctionName = funtion.FunctionName,
                    IgnoreControl = a.IgnoreControl
                });

            return View(actionModelList);
        }

        #endregion 方法
    }
}